﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace DH.MQ.Helper
{
    public class Config
    {
        //读
        public static string GetValue(string AppKey)
        {
            try
            {
                string AppKeyValue;
                AppKeyValue = System.Configuration.ConfigurationManager.AppSettings.Get(AppKey);
                return AppKeyValue;
            }
            catch (Exception ex)
            {
                throw new Exception("配置文件读取错误", ex);
            }
        }
        //读
        public static string GetValue(string AppKey, string defaultValue)
        {
            try
            {
                string AppKeyValue;
                AppKeyValue = System.Configuration.ConfigurationManager.AppSettings.Get(AppKey);
                if (AppKeyValue.Trim() == "")
                {
                    return defaultValue;
                }
                return AppKeyValue;
            }
            catch (Exception ex)
            {
                SetValue(AppKey, defaultValue);
                return defaultValue;
            }
        }
        //写
        public static void SetValue(string appKey, string appValue)
        {
            System.Configuration.ConfigurationManager.AppSettings.Set(appKey, appValue);
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(Process.GetCurrentProcess().MainModule.FileName + ".config");

            XmlNode xNode;
            XmlElement xElem1;
            XmlElement xElem2;
            xNode = xDoc.SelectSingleNode("//appSettings");

            xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']");
            if (xElem1 != null) xElem1.SetAttribute("value", appValue);
            else
            {
                xElem2 = xDoc.CreateElement("add");
                xElem2.SetAttribute("key", appKey);
                xElem2.SetAttribute("value", appValue);
                xNode.AppendChild(xElem2);
            }
            xDoc.Save(Process.GetCurrentProcess().MainModule.FileName + ".config");
        }
    }
}
